/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.utils.ConvertToHtml;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.merchant.MerchantClause;
import com.neusoft.lohas.modules.ivm.service.merchant.MerchantClauseService;

/**
 * 配送规则Controller
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/merchant/clause")
public class MerchantClauseController extends BaseController {

	@Autowired
	private MerchantClauseService merchantClauseService;
	
	@ModelAttribute
	public MerchantClause get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return merchantClauseService.get(id);
		}else{
			return new MerchantClause();
		}
	}
	
	@RequiresPermissions("ivm:merchant:clause:view")
	@RequestMapping(value = "form")
	public String form(MerchantClause merchantClause, Model model) {
		if (StringUtils.isBlank(merchantClause.getId())){
			merchantClause.setMerchant(super.merchant);
			merchantClause = merchantClauseService.findByMerchantId(merchantClause.getMerchant().getId());
		}
		model.addAttribute("merchantClause", merchantClause);
		return "modules/ivm/set/merchantClauseForm";
	}

	@RequiresPermissions("ivm:merchant:clause:edit")
	@RequestMapping(value = "save")
	public String save(MerchantClause merchantClause, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, merchantClause)){
			return form(merchantClause, model);
		}
		merchantClause.setContent(ConvertToHtml.convertor(merchantClause.getContent()));
		merchantClauseService.save(merchantClause);
		addMessage(redirectAttributes, "保存配送规则成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/merchant/clause/form";
	}
	
}
